package com.xinfu.newproject.model;


import android.util.Log;

import com.google.gson.Gson;
import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.pay.PayBean;
import com.xinfu.newproject.bean.pay.PayBody;
import com.xinfu.newproject.bean.pay.bean.PayCostBean;
import com.xinfu.newproject.bean.pay.body.PayCostBody;
import com.xinfu.newproject.callback.MyCallBack;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PayModel implements IModel.IPayModel {

    /**
     * 报修支付
     *
     * @param payBody
     * @param callBack
     */
    @Override
    public void pay(PayBody payBody, MyCallBack<PayBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("billType", payBody.getBillType());
        map.put("mobile", payBody.getMobile());
        map.put("payFee", payBody.getPayFee());
        map.put("payType", "online");
        map.put("repairOrder", payBody.getRepairOrder());
        map.put("roomId", payBody.getRoomId());
        map.put("userId", payBody.getRoomId());

        Map<String, Object> map1 = new HashMap<>();
        map1.put("tenantId", payBody.getHeader());

        Log.i("testPay", new Gson().toJson(map));
        JsNetworkService.getInstance1().getIJsNetworkService()
                .getPay(map1, map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PayBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull PayBean payBean) {

                        callBack.onSuccess(payBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 物业费支付
     *
     * @param
     * @param callBack
     */
    @Override
    public void payCost(PayCostBody payCostBody, MyCallBack<PayCostBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("billType", payCostBody.getBillType());
        map.put("mobile", payCostBody.getMobile());
        map.put("payFee", payCostBody.getPayFee());
        map.put("payType", "online");
        map.put("roomId", payCostBody.getRoomId());
        map.put("billids", payCostBody.getBillids());
        map.put("name", payCostBody.getName());
        map.put("userId", payCostBody.getUserId());
        if (payCostBody.getInvoiceAccount() != null) {
            map.put("invoiceAccount", payCostBody.getInvoiceAccount());
        }
        if (payCostBody.getInvoiceAddress() != null) {
            map.put("invoiceAddress", payCostBody.getInvoiceAddress());
        }
        if (payCostBody.getInvoiceEin() != null) {
            map.put("invoiceEin", payCostBody.getInvoiceEin());
        }
        if (payCostBody.getInvoiceEmail() != null) {
            map.put("invoiceEmail", payCostBody.getInvoiceEmail());
        }
        if (payCostBody.getInvoiceMobile() != null) {
            map.put("invoiceMobile", payCostBody.getInvoiceMobile());
        }
        if (payCostBody.getInvoiceName() != null) {
            map.put("invoiceName", payCostBody.getInvoiceName());
        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("tenantId", payCostBody.getHeader());

        Log.i("testPay", new Gson().toJson(map));
        JsNetworkService.getInstance3().getIJsNetworkService()
                .getPayCost(map1, map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PayCostBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull PayCostBean payCostBean) {

                        callBack.onSuccess(payCostBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
